Musical Features Extractor

How to use mfeat to extract music features.
In [1]:
import sys
sys.path.append("../src/mfeat/") # This is done to make the modules accessible to import
import modusa as ms
In [2]:
from high_cut_off import get_high_cutoff_freq
from loudness import get_loudness_contour
from rhythmicity import get_rhythmicity
from tempo import get_tempo
In [54]:
audio_path = "../../../music/songs/Atif Aslam, Arko - O Saathi.mp3" # Path of the audio file

local_loudness, local_loudness_t, title = get_loudness_contour(audio_path) # Computes loudness
local_cutoff, local_cutoff_t, agg_cutoff, title, S = get_high_cutoff_freq(audio_path) # Computes high cutoff freq
local_tempo, local_tempo_t, agg_tempo, confidence = get_tempo(audio_path) # Computes tempo
local_rhythmicity, local_rhythmicity_t, title = get_rhythmicity(audio_path) # Computes rhythmicity

Visualisation¶

In [55]:
display(
    ms.plot1d(
        (local_loudness, local_loudness_t),
        title=title,
        ylabel="Loudness (dB)",
        xlabel="Time (sec)",
        legend="Loudness contour"
    )
)

display(
    ms.plot2d(
        (S[0], S[1], S[2]),
        (local_cutoff, local_cutoff_t),
        title=title,
        ylabel="Frequency (Hz)",
        xlabel="Time (sec)",
        legend="High Cutoff"
    )
)

display(
    ms.plot1d(
        (local_tempo, local_tempo_t),
        title=title,
        ylabel="Tempo (BPM)",
        xlabel="Time (sec)",
        legend="Tempo"
    )
)

display(
    ms.plot1d(
        (local_rhythmicity, local_rhythmicity_t),
        title=title,
        ylabel="Strength",
        xlabel="Time (sec)",
        legend="Rhythmicity"
    )
)
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
No description has been provided for this image
In [56]:
y, sr, title = ms.load(audio_path)
ms.play(y, sr)
Out[56]:
Clip 1
Timing 0.00s → 251.82s
Player Your browser does not support the audio element.

How to create a dataframe for each of the features¶

In [12]:
import pandas as pd
In [25]:
# Create separate DataFrames
df_loudness = pd.DataFrame({
    "t": local_loudness_t,
    "loudness": local_loudness
})

df_cutoff = pd.DataFrame({
    "t": local_cutoff_t,
    "high_cutoff_freq": local_cutoff
})

df_tempo = pd.DataFrame({
    "t": local_tempo_t,
    "tempo": local_tempo
})

df_rhythmicity = pd.DataFrame({
    "t": local_rhythmicity_t,
    "rhythmicity": local_rhythmicity
})

How to save them use as csv¶

In [26]:
df_loudness.to_csv(f"{title}-loudness.csv", index=False)
df_cutoff.to_csv(f"{title}-cutoff.csv", index=False)
df_tempo.to_csv(f"{title}-tempo.csv", index=False)
df_rhythmicity.to_csv(f"{title}-rhythmicity.csv", index=False)

How to get the aggregated values¶

In [29]:
print("High cutoff: ", agg_cutoff, "Hz")
print("Global Tempo: ", agg_tempo, "BPM")
High cutoff:  16700.0 Hz
Global Tempo:  94 BPM